from . import view
from . import schema
from fastapi import APIRouter, Depends, Security
from package.connector.sql_db import db
from package.fastapi import permission

router_devops = APIRouter(prefix='/devops', tags=['运维对象'])
router_meta = {'module': '数据采集', 'sub_module': '运维对象'}

router_external = APIRouter(prefix='/external', tags=['外部数据集'])
router_external_meta = {'module': '数据采集', 'sub_module': '外部数据集'}


@router_devops.get('/', response_model=schema.PageDevops, openapi_extra=router_meta, name='devops_list_get',
                   summary='运维对象列表')
def devops_get(q: str = None,
               page: int = 1,
               page_size: int = 10,
               order_by: str = None,
               session=Depends(db.get_session),
               user=Security(permission.get_user_funcable, scopes=[])):
    return view.devops.get_page_items(session, q=q, page=page, page_size=page_size, order_by=order_by)


@router_devops.post('/', response_model=schema.DevopsOut, openapi_extra=router_meta, name='devops_post',
                    summary='运维对象创建')
def devops_post(body: schema.Devops,
                session=Depends(db.get_session),
                user=Security(permission.get_user_funcable, scopes=[])):
    return view.devops.create_item(session, body=body.dict())


@router_devops.get('/{id}', response_model=schema.DevopsOut, openapi_extra=router_meta, name='devops_get',
                   summary='运维对象详情')
def devops_get(id: str,
               session=Depends(db.get_session),
               user=Security(permission.get_user_funcable, scopes=[])):
    return view.devops.get_item(session, id=id)


@router_devops.patch('/{id}', response_model=schema.DevopsOut, openapi_extra=router_meta, name='devops_patch',
                     summary='运维对象修改')
def devops_patch(id: str,
                 body: schema.EditDevops,
                 session=Depends(db.get_session),
                 user=Security(permission.get_user_funcable, scopes=[])):
    return view.devops.update_item(session, body.dict(exclude_unset=True), id=id)


@router_devops.delete('/', openapi_extra=router_meta, name='devops_delete', summary='数据源删除')
def devops_delete(body: schema.BatchId,
                  session=Depends(db.get_session),
                  user=Security(permission.get_user_funcable, scopes=[])):
    return {'count': view.devops.delete_items(session, body.ids)}


# 外部数据源
@router_external.get('/', response_model=schema.PageExternal, openapi_extra=router_meta, name='external_list_get',
                     summary='数据集列表')
def external_get(q: str = None,
                 page: int = 1,
                 page_size: int = 10,
                 order_by: str = None,
                 session=Depends(db.get_session),
                 user=Security(permission.get_user_funcable, scopes=[])):
    return view.devops.get_page_items(session, q=q,
                                      filter_=[view.devops.model.category.in_(['Mysql', 'Postgres'])],
                                      page=page,
                                      page_size=page_size, order_by=order_by)
